#!/bin/bash
unzip rongshu.zip -d /app/pmapp/data/

zk01="server.1=10.102.35.153:2888:3888"
zk02="server.2=10.102.35.154:2888:3888"
zk03="server.3=10.102.35.155:2888:3888"

function replace_zk_common() {
    cd /app/pmapp/data/rongshu/apache-zookeeper-3.8.4-bin/conf/
    echo "$zk01" | tee -a zoo.cfg  >> zoo.cfg
    echo "$zk02" | tee -a zoo.cfg  >> zoo.cfg
    echo "$zk03" | tee -a zoo.cfg  >> zoo.cfg
}

function replace_pulsar_common() {
    cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/conf/
    echo "replace bookkeeper.conf"
    sed -i '649s/.*/metadataServiceUri=metadata-store:zk:10.102.35.153:2181,10.102.35.154:2181,10.102.35.155:2181/' bookkeeper.conf
    echo "replace broker.conf"
    sed -i '27s/.*/metadataStoreUrl=zk:10.102.35.153:2181,10.102.35.154:2181,10.102.35.155:2181/' broker.conf
    echo "replace client.conf"
    sed -i '25s/.*/webServiceUrl=http:\/\/10.102.35.153:8080,10.102.35.154:8080,10.102.35.155:8080\//' client.conf
    sed -i '30s/.*/brokerServiceUrl=pulsar:\/\/10.102.35.153:6650,10.102.35.154:6650,10.102.35.155:6650\//' client.conf
}

function replace_pulsar_manage() {
    cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/pulsar-manager/pulsar-manager
    echo "replace application.properties"
    sed -i '133s/.*/default.environment.service_url=http:\/\/10.102.35.153:9080/' application.properties
    sed -i '133s/.*/default.environment.bookie_url=pulsar:\/\/10.102.35.153:7650/' application.properties
}

function replace_zk_a_conf() {
  cd /app/pmapp/data/rongshu/apache-zookeeper-3.8.4-bin/tmp/zookeeper/
  echo 1 > myid
}

function replace_pulsar_a_conf() {
    cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/conf/
    echo "replace bookkeeper.conf"
    sed -i '56s/.*/advertisedAddress=10.102.35.153/' bookkeeper.conf
    echo "replace broker.conf"
    sed -i '61s/.*/advertisedAddress=10.102.35.153/' broker.conf
    echo "replace proxy.conf"
    sed -i '26s/.*/metadataStoreUrl=zk:10.102.35.153:2181,10.102.35.154:2181,10.102.35.155:2181/' proxy.conf
    sed -i '57s/.*/advertisedAddress=10.102.35.153/' proxy.conf
    sed -i '326s/.*/zookeeperServers=10.102.35.153:2181,10.102.35.154:2181,10.102.35.155:2181/' proxy.conf
}

function replace_zk_b_conf() {
  cd /app/pmapp/data/rongshu/apache-zookeeper-3.8.4-bin/tmp/zookeeper/
  echo 2 > myid
}

function replace_pulsar_b_conf() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/conf/
  echo "replace bookkeeper.conf"
  sed -i '56s/.*/advertisedAddress=10.102.35.154/' bookkeeper.conf
  echo "replace broker.conf"
  sed -i '61s/.*/advertisedAddress=10.102.35.154/' broker.conf
}

function replace_zk_c_conf() {
  cd /app/pmapp/data/rongshu/apache-zookeeper-3.8.4-bin/tmp/zookeeper/
  echo 3 > myid
}

function replace_pulsar_c_conf() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/conf/
  echo "replace bookkeeper.conf"
  sed -i '56s/.*/advertisedAddress=10.102.35.155/' bookkeeper.conf
  echo "replace broker.conf"
  sed -i '61s/.*/advertisedAddress=10.102.35.155/' broker.conf
}

function replace_pulsar_manage() {
    cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/pulsar-manager/pulsar-manager
    echo "replace application.properties"
    sed -i '133s/.*/default.environment.service_url=http:\/\/10.91.37.47:9080/' application.properties
    sed -i '133s/.*/default.environment.bookie_url=pulsar:\/\/10.91.37.47:7650/' application.properties
}

# pulsar_a 节点一配置文件 有proxy服务 修改
function pulsar_a(){
  replace_zk_common
  replace_zk_a_conf
  replace_pulsar_common
  replace_pulsar_a_conf
  replace_pulsar_manage

}

# pulsar_a 节点二配置文件 有pulsar_manager服务 修改
function pulsar_b(){
  replace_zk_common
  replace_zk_b_conf
  replace_pulsar_common
  replace_pulsar_b_conf
  replace_pulsar_manage
}

# pulsar_a 节点三配置文件 修改
function pulsar_c(){
  replace_zk_common
  replace_zk_c_conf
  replace_pulsar_common
  replace_pulsar_c_conf
  replace_pulsar_manage
}

# 如何使用
case $1 in
1)
  pulsar_a
;;
2)
  pulsar_b
;;
3)
  pulsar_c
;;
*)
echo "pulsar_a集群节点配置文件修改 $0 1(节点a)/2(节点b)/3(节点c)"
;;
esac
